<template>
  <div class="login">
    <LoginLeftView></LoginLeftView>
    <div class="right-wrap">
      <div class="top-right-wrap">
        <div class="btn theme-btn" @click="toggleTheme">
          <i class="iconfont-sys">
            {{ isDark ? '&#xe6b5;' : '&#xe725;' }}
          </i>
        </div>
      </div>
      <div class="header">
        <ArtLogo class="icon" />
        <h1>{{ systemName }}</h1>
      </div>
      <div class="login-wrap">
        <div class="form">
          <h3 class="title">欢迎回来</h3>
          <p class="sub-title">输入您的账号和密码登录</p>
          <ElForm
            ref="formRef"
            :model="formData"
            :rules="rules"
            @keyup.enter="handleSubmit"
            style="margin-top: 25px"
          >
            <ElFormItem prop="tenant_code">
              <ElInput
                placeholder="请输入租户编码"
                size="large"
                v-model.trim="formData.tenant_code"
              />
            </ElFormItem>
            <ElFormItem prop="account">
              <ElInput placeholder="请输入账号" size="large" v-model.trim="formData.account" />
            </ElFormItem>
            <ElFormItem prop="password">
              <ElInput
                placeholder="请输入密码"
                size="large"
                v-model.trim="formData.password"
                type="password"
                radius="8px"
                autocomplete="off"
                show-password
              />
            </ElFormItem>
            <ElFormItem prop="captcha">
              <div class="captcha-wrap">
                <ElInput placeholder="请输入验证码" size="large" v-model.trim="formData.captcha" />
                <img
                  :src="captchaImageUrl"
                  @click="refreshCaptcha"
                  class="captcha-image"
                  alt="验证码图片"
                  v-if="captchaImageUrl"
                  @error="handleImageError"
                />
                <div v-else class="captcha-loading" @click="refreshCaptcha">加载中...</div>
              </div>
            </ElFormItem>
            <div class="forget-password">
              <ElCheckbox v-model="formData.rememberPassword">记住密码</ElCheckbox>
              <router-link to="/auth/forget-password">忘记密码</router-link>
            </div>

            <div style="margin-top: 30px">
              <ElButton
                class="login-btn"
                size="large"
                type="primary"
                @click="handleSubmit"
                :loading="loading"
              >
                登录
              </ElButton>
            </div>
          </ElForm>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import AppConfig from '@/config'
  import { ElForm, ElMessage, ElNotification } from 'element-plus'
  import { useUserStore } from '@/store/modules/user'
  import { SystemThemeEnum } from '@/enums/appEnum'
  import { useSettingStore } from '@/store/modules/setting'
  import type { FormInstance, FormRules } from 'element-plus'
  import { onMounted, ref, reactive, computed } from 'vue'
  import { getCaptcha, userLogin, getUserInfo } from '@/api/system/api'
  import defaultAvatar from '@/assets/img/user/avatar.png'
  defineOptions({ name: 'Login' })
  const settingStore = useSettingStore()
  const { isDark, systemThemeType } = storeToRefs(settingStore)

  const userStore = useUserStore()
  const router = useRouter()

  const systemName = AppConfig.systemInfo.name
  const formRef = ref<FormInstance>()
  const formData = reactive({
    tenant_code: 'default',
    account: '',
    password: '',
    rememberPassword: true,
    captcha: ''
  })

  const rules = computed<FormRules>(() => ({
    tenant_code: [{ required: true, message: '请输入租户编码', trigger: 'blur' }],
    account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    captcha: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
  }))

  const loading = ref(false)

  const captchaImageUrl = ref('') // 验证码图片的URL
  const captchaImageID = ref('') // 验证码图片的ID

  const handleSubmit = async () => {
    if (!formRef.value) return

    await formRef.value.validate(async (valid) => {
      if (valid) {
        loading.value = true
        // 延时辅助函数
        const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
        try {
          const res = await userLogin({
            tenant_code: formData.tenant_code,
            account: formData.account,
            password: formData.password,
            captcha: formData.captcha,
            captcha_id: captchaImageID.value
          })
          if (res.access_token) {
            // 设置 token
            userStore.setToken(res.access_token)
            // 设置当前租户编码
            userStore.setCurrentTenantCode(formData.tenant_code)
            // 获取用户信息
            const userRes = await getUserInfo()
            if (userRes) {
              console.log('获取用户信息成功:', userRes)
              // 使用导入的图片路径
              userRes.avatar = defaultAvatar
              userStore.setUserInfo(userRes)
            } else {
              ElMessage.error('获取用户信息失败')
              console.error('获取用户信息失败:', userRes)
            }
            // 设置登录状态
            userStore.setLoginStatus(true)
            // 延时辅助函数
            await delay(1000)
            // 登录成功提示
            showLoginSuccessNotice()
            // 跳转首页
            console.log('登录成功，跳转首页')
            router.push('/')
          } else {
            ElMessage.error('登录失败')
            refreshCaptcha()
          }
        } catch (error) {
          // 处理登录过程中的异常
          console.error('登录过程发生错误:', error)

          // 直接显示API返回的错误消息，如果有的话
          if (error instanceof Error) {
            // 直接显示错误消息，这个消息现在包含了API返回的真实错误信息
            ElMessage.error(error.message || '登录失败，请重试')
          } else {
            ElMessage.error('登录失败，请重试')
          }
          refreshCaptcha()
        } finally {
          await delay(1000)
          loading.value = false
        }
      }
    })
  }

  // 登录成功提示
  const showLoginSuccessNotice = () => {
    setTimeout(() => {
      ElNotification({
        title: '登录成功',
        type: 'success',
        showClose: true,
        duration: 2500,
        zIndex: 10000,
        message: `欢迎回来!`
      })
    }, 300)
  }

  // 切换主题
  import { useTheme } from '@/composables/useTheme'

  const toggleTheme = () => {
    let { LIGHT, DARK } = SystemThemeEnum
    useTheme().switchThemeStyles(systemThemeType.value === LIGHT ? DARK : LIGHT)
  }

  const refreshCaptcha = async () => {
    try {
      const captchaData = await getCaptcha(80, 240)
      console.log('验证码数据:', captchaData) // 调试日志

      // 检查数据结构并正确赋值
      if (captchaData && captchaData.id && captchaData.image) {
        captchaImageUrl.value = captchaData.image
        captchaImageID.value = captchaData.id
        console.log('验证码图片URL:', captchaImageUrl.value)
        console.log('验证码ID:', captchaImageID.value)
      } else {
        console.error('验证码数据结构异常:', captchaData)
        ElMessage.error('验证码数据格式错误')
      }
    } catch (error) {
      console.error('Error refreshing captcha:', error)
      ElMessage.error('验证码获取失败')
    }
  }

  const handleImageError = () => {
    console.error('验证码图片加载失败')
    ElMessage.error('验证码图片加载失败，请点击刷新')
  }

  onMounted(() => {
    refreshCaptcha() // 页面加载时获取验证码
  })
</script>

<style lang="scss" scoped>
  @use './index';
</style>
